package com.netease.recyclerviewslidemenu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * @Description TODO
 * @Date 2019-08-11 13:29
 * @Created by caoyu
 */
public class SlideMenuView extends ViewGroup {
    private int mMenuWidth;

    public SlideMenuView(Context context) {
        this(context, null);
    }

    public SlideMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        final int count = getChildCount();
        if (count > 2) {
            throw new RuntimeException("只能有两个直接子布局");
        }

//        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int maxHeight = 0;
        int maxWidth = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int height = child.getMeasuredHeight();
            int width = child.getMeasuredWidth();
            Log.d(this.getClass().getName(), "onMeasure: ");
            maxHeight = Math.max(maxHeight, height);
            maxWidth += width;
        }
        Log.d(this.getClass().getName(), "onMeasure: maxWidth " + maxWidth);
        Log.d(this.getClass().getName(), "onMeasure: maxHeight " + maxHeight);

        setMeasuredDimension(maxWidth, maxHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int leftPos = getPaddingLeft();
        int rightPos = r - l - getPaddingRight();

        for (int i = 0; i < 2; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();
                int left = leftPos + params.leftMargin;
                int right = rightPos - params.rightMargin;

                child.layout(left, getPaddingTop(), right, height);
                leftPos += width;
                if (i == 1) {
                    mMenuWidth = right;
                }
            }

        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p != null;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d(this.getClass().getName(), "onInterceptTouchEvent: "+MotionEvent.ACTION_DOWN);
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(this.getClass().getName(), "onInterceptTouchEvent: "+MotionEvent.ACTION_MOVE);
                break;
            case MotionEvent.ACTION_UP:
                Log.d(this.getClass().getName(), "onInterceptTouchEvent: "+MotionEvent.ACTION_UP);
                break;
        }
        return true;
    }

    private int downX;
    private int downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        Log.d(this.getClass().getName(), "onTouchEvent:x " + x);
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = x;
                downY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - downX;
                int offsetY = y - downY;
                scrollBy(-offsetX, -offsetY);
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
